package com.ssyc.student.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.Scroller;

/**
 * 实现平滑移动的自定义ImageView
 * Created by Administrator on 2018/4/25 0025.
 */

@SuppressLint("AppCompatCustomView")
public class ScrollerImageView extends ImageView {
    private Scroller scroller;

    public ScrollerImageView(Context context) {
        super(context);
    }

    public ScrollerImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller = new Scroller(context);
    }


    //缓慢滚动到指定位置，调用该方法即可
    public void smoothScrollTo(int destX, int destY, int duration) {


        int scrollX = getScrollX();
        int deltaX = destX - scrollX;

        int scrollY = getScrollY();
        int deltaY = destY - scrollY;

        scroller.startScroll(scrollX, 0, deltaX, deltaY, duration * 1000);
        invalidate();

    }

    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            postInvalidate();
        }
    }

}
